package com.petmarket.controller;

import com.alibaba.fastjson.JSON;
import com.petmarket.client.PetClient;
import com.petmarket.common.PageResult;
import com.petmarket.common.Result;
import com.petmarket.entity.Pet;
import com.petmarket.entity.PetRequest;
import com.petmarket.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 宠物Controller
 */
@RestController
@RequestMapping("/Pet")
@Slf4j
public class PetContoller {

    @Autowired
    private PetClient petClient;

    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody Pet pet){
        try{
            if (log.isInfoEnabled()) {
                log.info("PetController.insert:{}", JSON.toJSONString(pet));
            }
            petClient.insert(pet);
            return Result.ok("新增成功");
        }
        catch (Exception e){
            return Result.fail("新增失败");
        }
    }

    @PostMapping("/insert2")
    public Result<Boolean> insert2(@RequestBody PetRequest petRequest){
        try{
            if (log.isInfoEnabled()) {
                log.info("PetController.insert:{}", JSON.toJSONString(petRequest));
            }
            petClient.insert(petRequest.getPet(),petRequest.getHexPrivateKey());
            return Result.ok("新增成功");
        }
        catch (Exception e){
            return Result.fail("新增失败");
        }
    }

    @PostMapping("/query")
    public Result<PageResult<Pet>> query(@RequestBody Pet pet){
        try{
            if (log.isInfoEnabled()) {
                log.info("PetController.insert:{}", JSON.toJSONString(pet));
            }
            Pet resPet = petClient.query(pet);
            PageResult<Pet> result=new PageResult<>();
            List<Pet> list = new ArrayList<>();
            list.add(resPet);
            result.setRecords(list);
            return Result.ok(result);
        } catch (Exception e) {
            return Result.fail("查询失败");
        }
    }

    @PostMapping("/update")
    public Result<Pet> update(@RequestBody Pet pet){
        try{
            if (log.isInfoEnabled()) {
                log.info("PetController.insert:{}", JSON.toJSONString(pet));
            }
            petClient.edit(pet);
            return Result.ok("更新成功");
        }catch (Exception e){
            return Result.fail("更新失败");
        }
    }

    @PostMapping("/update2")
    public Result<Pet> update2(@RequestBody PetRequest petRequest){
        try{
            if (log.isInfoEnabled()) {
                log.info("PetController.update2:{}", JSON.toJSONString(petRequest));
            }
            petClient.edit(petRequest.getPet(),petRequest.getHexPrivateKey());
            return Result.ok("更新成功");
        }catch (Exception e){
            return Result.fail("更新失败");
        }
    }

    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody Pet pet){
        try{
            if (log.isInfoEnabled()) {
                log.info("PetController.delete:{}", JSON.toJSONString(pet));
            }
            petClient.remove(pet);
            return Result.ok("删除成功");
        }
        catch (Exception e){
            return Result.fail("删除失败");
        }
    }

    @PostMapping("/getPetPage")
    public Result<PageResult<Pet>> getPetPage(@RequestBody Pet pet){
        try{
            if (log.isInfoEnabled()){
                log.info("UserController.getUserPage:{}", JSON.toJSONString(pet));
            }
            PageResult<Pet> result = petClient.getPetPage(pet.getPageNo(),pet.getPageSize());
            result.setTotal(petClient.countPet());
            return Result.ok(result);
        }
        catch (Exception e){
            return Result.fail("获取列表失败");
        }
    }
}
